<?php
/**
 * Created by PhpStorm.
 * User: caicm
 * Date: 2017/3/7
 * Time: 10:15
 */

namespace SDXApi\Service;


class OpensearchService {
    /*
     * 获取相关级别分类
     */
    public function get_category_by_level($category_list, $level=1){
        $list = array();
        foreach($category_list as $key_1=>$v){

            if($level == 1){
                $list[$key_1]['cat_id'] = $v['cat_id'];
                $list[$key_1]['cat_name'] = $v['cat_name'];
            }else{

                foreach($v['children'] as $key_2=>$l){

                    if($level == 2){
                        $list[$key_1.$key_2]['cat_id'] = $l['cat_id'];
                        $list[$key_1.$key_2]['cat_name'] = $l['cat_name'];
                    }else{

                        foreach($l['children'] as $key_3=>$k){
                            $list[$key_1.$key_2.$key_3]['cat_id'] = $k['cat_id'];
                            $list[$key_1.$key_2.$key_3]['cat_name'] = $k['cat_name'];
                        }
                    }

                }
            }

        }
        return $list;
    }

    /*
     * 获取三级分类
     */
    public function get_son_category($category_list, $cat_id=0){
        $list = array();
        foreach($category_list as $v){
            foreach($v['children'] as $l){
                foreach($l['children'] as $k){
                    if($cat_id){
                        if($k['parent_id'] == $cat_id || $l['parent_id'] == $cat_id){
                            $list[] = $k;
                        }
                    }else{
                        $list[] = $k;
                    }
                }
            }
        }
        return $list;
    }

    /*
     * 获取分类列表
     */
    public function get_category_list($map){
        $category_id = $this->get_category_son_id($map);
        $category_list = $this->assoc_unique($category_id, 'cat_id');
        return $this->treeData($category_list);
    }

    /*
     * 获取子分类列表
     */
    public function get_son_category_list($map){

        return M('category')->field('cat_id,cat_name')->where($map)->select();

    }


    //树形数组
    protected function treeData($data,$pid = 0){
        $result = array();
        foreach($data as $v){
            if($v['parent_id'] == $pid){
                $v['children'] = $this->treeData($data,$v['cat_id']);
                $result[] = $v;
            }
        }
        return $result;
    }


    //递归获取所有父级id
    protected function get_category_son_id($map){
        global $category_list;
        $category = M('category')->field('cat_id,cat_name,parent_id')->where($map)->select();

        foreach ($category as $value){
            $category_list[] = $value;
            if($value['parent_id']){
                $this->get_category_son_id('is_show!=0 and cat_id='.$value['parent_id']);
            }
        }
        return $category_list;

    }

    //过滤重复
    protected function assoc_unique($arr, $key) {
        $tmp_arr = array();

        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                unset($arr[$k]);
            } else {
                $tmp_arr[] = $v[$key];
                $list[] = $arr[$k];
            }
        }

        return $list;
    }

    public function add_search_keyword_pulldown($max_num){
        $sql = "SELECT keyword,COUNT(id) as num FROM `sdx_search_keyword` where 1 GROUP BY `keyword` HAVING num > {$max_num}";
        $search_keyword = M()->query($sql);

        foreach ($search_keyword as $value) {
            $keyword = '';
            $data = array();
            $pulldown_id = 0;
            $keyword = trim($value['keyword']);

            //不带中文跳出
            if (!preg_match("/[\x7f-\xff]/", $keyword)) {
                continue;
            }

            $data['keyword'] = $keyword;
            $data['num'] = $value['num'];

            //关键词是否存在
            $pulldown_id = M('search_keyword_pulldown')->where(array('keyword'=>$keyword))->getField('id');

            if($pulldown_id){
                M('search_keyword_pulldown')->where(array('id'=>$pulldown_id))->save($data);
            }else{
                M('search_keyword_pulldown')->add($data);
            }

        }
    }

    /**
     * @param int $uid
     * @param int $is_no_del
     * @return mixed
     */
    public function get_history($uid=0, $is_no_del=0){
        if($is_no_del == 1){
            $where = "AND is_history_del = 0";
        }
        $sql = "SELECT
                    keyword
                FROM
                    (
                        SELECT
                            *
                        FROM
                            sdx_search_keyword
                        WHERE
                            uid = {$uid}
                        AND search_type != 2
                            {$where}
                        ORDER BY
                            id DESC
                    ) tmp
                GROUP BY
                    `keyword`
                ORDER BY
                    id DESC
                LIMIT 20;";

        return M()->query($sql);

    }

    public function get_history_catname(){

    }

    /**
     * @return mixed
     */
    public function get_hot(){

        $key = "hot_search_data";
        $data = S($key);

        if(!$data){
            //获取7天的数据
            $max_time = gmtime()-86400*7;

            $sql = "SELECT keyword,
                       COUNT(id) as num
                  FROM sdx_search_keyword
                 where search_type!= 2
                   and create_time> {$max_time}
                   and `goods_num` > 0
                 GROUP BY `keyword`
                 ORDER BY num DESC
                 LIMIT 20;";
            $data = M()->query($sql);
            S($key, $data, 3600);
        }

        return $data;
    }

    public function get_search_hot_keyword($uid=0){
        $list = array();
        $hot_keyword = array();

        //搜索历史记录分类
        $search_history = $this->get_history($uid, 0);
        foreach ($search_history as $key=>$val){
            $cat_name = A('Opensearch')->search_keyword_dispose($val['keyword']);
            if(!in_array($cat_name, $list) && $cat_name){
                $list[] = $cat_name;
                $hot_keyword[] = array(
                    'id' => 1,
                    'name' => $cat_name,
                    'is_extrude' => 0
                );
            }
            if(count($list) >3){
                break;
            }
        }

        //获取近7天搜索最多的词
        $hot_data = $this->get_hot();
        $i = 1;
        foreach ($hot_data as $key=>$val){
            if(!in_array($val['keyword'], $list) && $val['keyword']){
                $list[] = $val['keyword'];
                $hot_keyword[] = array(
                    'id' => 2,
                    'name' => $val['keyword'],
                    'is_extrude' => 0
                );
                $i++;
            }
            if($i >4){
                break;
            }
        }

        //热门搜索词
        $searchHotModel = D('SearchHot');
        $search_hots = $searchHotModel->get_search_hots();
        foreach ($search_hots as $key=>$val){
            if(!in_array($val['name'], $list) && $val['name']){
                $hot_keyword[] = $val;
            }
        }

        return array_slice($hot_keyword, 0, 20);

    }

}